#include <iostream>
using namespace std;

const int N = 1e5 + 5;

int trie[N][26], cnt[N], idx;

void insert(char* str)
{
    int p = 0;
    for(int i = 0; str[i]; ++i)
    {
        int cur = str[i] - 'a';
        if(trie[p][cur] == 0) trie[p][cur] = ++idx;
        p = trie[p][cur];
    }
    cnt[p]++;
}

int qurey(char* str)
{
    int p = 0;
    for(int i = 0; str[i]; ++i)
    {
        int cur = str[i] - 'a';
        if(trie[p][cur] == 0) return 0;
        p = trie[p][cur];
    }
    return cnt[p];
}

int main()
{
    int n; cin >> n;
    char c, str[N];
    while(n--)
    {
        cin >> c >> str;
        if(c == 'I') insert(str);
        else cout << qurey(str) << endl;
    }
    return 0;
}